package com.ys.service.storage.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.AddDto;
import com.ys.entity.storage.*;
import com.ys.entity.sys.YsCol;
import com.ys.mapper.storage.*;
import com.ys.service.storage.TccLogisticsChannelDetailService;
import com.ys.util.CheckUtil;
import com.ys.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-12-05
 * @Description: 物流渠道详情
 * @Version: 1.5
 */
@Service("tccLogisticsChannelDetailService")
public class TccLogisticsChannelDetailServiceImpl implements TccLogisticsChannelDetailService {
    private static final Logger logger = LoggerFactory.getLogger(TccLogisticsChannelDetailServiceImpl.class);
    @Autowired
    private StorageMapper storageMapper;

    @Autowired
    private  TccLogisticsChannelDetailMapper tccLogisticsChannelDetailMapper;

    @Autowired
    private TccAdditionalBillingMapper tccAdditionalBillingMapper;

    @Autowired
    private CheckUtil checkUtil;
    /**
     * 新增
     */
    @Override
    @Transactional(timeout = 120, propagation = Propagation.REQUIRES_NEW)
    public R add(Map<String, Object> params, Long menuId) throws Exception {
        logger.info("新增传参:" + params);
        //参数验证
        String errorBack = checkParams(params, menuId);
        if (errorBack != null && !"".equals(errorBack)) {
            return R.error(errorBack);
        }
        TccLogisticsChannelDetail tccLogisticsChannelDetail = JSON.parseObject(JSON.toJSONString(params), TccLogisticsChannelDetail.class);
        TccWarehouse tccWarehouse = JSON.parseObject(JSON.toJSONString(params), TccWarehouse.class);

        Map<String, Object> tccLogisticsChannelDetail1 = new TccLogisticsChannelDetail()

                .queryFirstMap();
        JSONObject jsonObject = JSONObject.parseObject(params.get("subData").toString());//获取列表信息

        String logsticsChannelName = "";
        String vck = "";


        tccLogisticsChannelDetail.setLogsticsChannelName(logsticsChannelName);


        //新增
        tccLogisticsChannelDetailMapper.insert(tccLogisticsChannelDetail);




        return R.ok(AddDto.gotoEdit("/web/storage/tccLogisticsChannelDetail/toEdit?id=" + tccLogisticsChannelDetail.getId()));
    }

    /**
     * 保存
     */
    @Override
    public R save(Map<String, Object> params, Long menuId) throws Exception {
        logger.info("物流服务维护管理保存传参："+params);

        // 参数验证
        String errorMsg = checkParams(params,menuId);
        if(errorMsg!=null && !"".equals(errorMsg)){
            return R.error(errorMsg);
        }
        TccLogisticsChannelDetail tccLogisticsChannelDetail = JSON.parseObject(JSON.toJSONString(params), TccLogisticsChannelDetail.class);
        TccWarehouse tccWarehouse = JSON.parseObject(JSON.toJSONString(params), TccWarehouse.class);

        Map<String, Object> tccLogisticsChannelDetail1 = new TccLogisticsChannelDetail()

                .queryFirstMap();
        //判断是否唯一
        String providerCode=params.get("providerCode").toString();


        String logsticsChannelName = "";

        tccLogisticsChannelDetail.setLogsticsChannelName(logsticsChannelName);

        JSONObject jsonObject = JSONObject.parseObject(params.get("subData").toString());//获取列表信息


        return R.ok(AddDto.gotoEdit("/web/storage/tccLogisticsChannelDetail/toEdit?id=" + tccLogisticsChannelDetail.getId()));

    }



    /**
     * 参数验证
     */
    public String checkParams(Map<String, Object> params, Long menuId) throws Exception {
        //获取pc字段数据
        List<YsCol> listField = new YsCol()
                .where("A.MenuId = " + menuId)
                .setOrderby("A.SortNo ASC")
                .queryList();
        //验证必填字段的非空；其他字段的长度限制、类型
        String backMsg = checkUtil.checkTableColLengthAndDataType("TCC_LogisticsChannelDetail", listField, params, ysColItem -> ysColItem.getFormEditRequired() != null && ysColItem.getFormEditRequired() == 1);
        return backMsg;
    }


    //根据主表bid获取子表信息
    @Override
    @Transactional(timeout = 120, propagation = Propagation.REQUIRES_NEW)
    public List<Map<String, Object>> getItemListById(Long id) throws Exception {
        //获取子表信息
        List<Map<String, Object>> itemList = new TccPreAllocatePublishItem()
                .queryMap();
        return itemList;
    }

    @Override
    public R editDetailBatch(Long LogisticsChannelDetailId, String detailArr) {

        if (LogisticsChannelDetailId == null) {
            return R.error("提交的参数有误，请联系客服");
        }
        TccLogisticsChannelDetail tccLogisticsChannelDetail = new TccLogisticsChannelDetail().setId(LogisticsChannelDetailId).queryFirst();

        JSONArray detailJA = JSON.parseArray(detailArr);

        //验证数据是否已存在
        List<String> listUnicode = new ArrayList<String>();
        for (int i = 0; i < detailJA.size(); i++) {
            JSONObject detailJO = detailJA.getJSONObject(i);
            String startheavy = detailJO.getString("startheavy");
            String heavyFee = detailJO.getString("heavyFee");
            // 添加调货信息
            BigDecimal originPriceMin = BigDecimal.ZERO;
            BigDecimal originPriceMax = BigDecimal.ZERO;
            return R.ok();
        }
        return null;
    }

    // 获取列表数据
    @Override
    @Transactional
    public Map<String,Object> getExtraParam(String bid) throws Exception{
        Map<String, Object> entity = new TccLogisticsChannelDetail().setBid(bid)
                .queryFirstMap();
        entity.put("status", 1);


        return entity;
    }



    //删除
    @Override
    public int del(long id) throws Exception{
        tccLogisticsChannelDetailMapper.deleteTrue(id);
        return 1;
    }


}
